import { Config, Inject, Provide } from '@midwayjs/decorator';
import { BaseService, CoolCommException } from '@cool-midway/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { UserInfoEntity } from '../entity/info';
import { UserAdEntity } from '../entity/adInfo';
import { Context } from '@midwayjs/koa';

/**
 * 广告播放
 */
@Provide()
export class UserAdService extends BaseService {

  @InjectEntityModel(UserAdEntity)
  UserAdEntity: Repository<UserAdEntity>;

  @InjectEntityModel(UserInfoEntity)
  UserInfoEntity: Repository<UserInfoEntity>;

  @Inject()
  ctx: Context;


  /**
   * 修改之后
   * @param data
   * @param type
   */
  async modifyAfter(data: any, type: 'add' | 'update' | 'delete') {
    console.log(data)
    if (type == 'add') {
      const info = await this.UserInfoEntity.findOneBy({ id: data.userId });
      if ((data.status == 1||data.status == 2)&&info.adNumber>0) {
        // 将其他的版本设置为禁用
        await this.UserInfoEntity.update(
          { id: info.id },
          { adNumber: info.adNumber - 1 }
        );
      }
    }
  }
}
